package com.co.server.service.user.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.co.common.dao.BaseDao;
import com.co.common.service.BaseServiceImpl;
import com.co.common.utils.EmailUtil;
import com.co.server.constant.Constant;
import com.co.server.dao.user.TUserActiveDao;
import com.co.server.entity.user.TUserActive;
import com.co.server.entity.user.aide.TUserActiveConstant;
import com.co.server.entity.user.aide.TUserActiveQuery;
import com.co.server.service.user.TUserActiveService;

@Service
public class TUserActiveServiceImpl extends BaseServiceImpl<TUserActive, Integer> implements TUserActiveService {
    @Resource
    private TUserActiveDao tUserActiveDao;

    @Override
    protected BaseDao<TUserActive, Integer> getBaseDao() {
        return tUserActiveDao;
    }


	public TUserActive queryLastByEmail(String email) {
		TUserActiveQuery query = new TUserActiveQuery();
		query.setEmail(email);
		query.setIsUse(Constant.NO);
		Sort sort = new Sort(Direction.DESC,"t.id");
		return queryOne(query,sort);
	}

	public void addBindCodeByEmail(String email, String userName ,String userIp) {
		String emailCode = RandomStringUtils.randomNumeric(6);
		TUserActive userActive = new TUserActive();
		userActive.setEmail(email); //
		userActive.setVcode(emailCode);
		userActive.setUserIp(userIp);
		userActive.setCreateTime(new Date());
		userActive.setIsUse(Constant.NO);
		userActive.setType(TUserActiveConstant.FLAG_EMAIL.intValue());
		add(userActive);
		EmailUtil.sendEmailCode(email,userName,emailCode);
	}
}